package com.information.manage.basic.common.result;

import com.information.manage.basic.common.result.enums.ResEnum;
import lombok.Data;

/**
 * 返回值工具类
 * @param <T>
 */
@Data
public class Result<T> {

    private Integer code;

    private String msg;

    private T data;

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(ResEnum resEnum) {
        this.code = resEnum.getCode();
        this.msg = resEnum.getMsg();
    }


    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result<T>(ResEnum.SUCCESS);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> ok() {
        return new Result<T>(ResEnum.SUCCESS);
    }

    public Result(T data) {
        this.data = data;
    }

    public static <T> Result<T> normalResponse(ResEnum resEnum) {
        Result<T> result = new Result<>();
        result.setCode(resEnum.getCode());
        result.setMsg(resEnum.getMsg());
        return result;
    }

    public Result() {
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Result<T> success() {
        Result<T> r = new Result<>();
        r.setData(data);
        r.setCode(ResEnum.SUCCESS.getCode());
        r.setMsg(ResEnum.SUCCESS.getMsg());
        return r;
    }

    public Result<T> success(String mag) {
        Result<T> r = new Result<>();
        r.setData(data);
        r.setCode(ResEnum.SUCCESS.getCode());
        r.setMsg(mag);
        return r;
    }

    public static <T> Result<T> fail() {
        return new Result<T>(ResEnum.BAD_REQUEST);
    }


    public static <T> Result<T> fail(String msg) {
        Result<T> r = new Result<>();
        r.setCode(ResEnum.BAD_REQUEST.getCode());
        r.setMsg(msg);
        return r;
    }

    public static <T> Result<T> fail(ResEnum resEnum) {
        Result<T> r = new Result<T>();
        r.setCode(resEnum.getCode());
        r.setMsg(resEnum.getMsg());
        return r;
    }

    public static <T> Result<T> fail(Integer code, String msg) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static <T> Result<T> fail(Integer code, String msg, T data) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}